/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.capex.javaee.sockets;


import java.net.*;

public class UDPServer {

    public static void main(String args[]) throws Exception {

        DatagramSocket udpserversocket = new DatagramSocket(7777);

        while (true) {

            byte[] receivedata = new byte[1024];
            byte[] senddata = new byte[1024];
            DatagramPacket receivepacket = new DatagramPacket(receivedata, receivedata.length);
            System.out.println("waiting for client");
            udpserversocket.receive(receivepacket);
            String clientMessage = (new String(receivepacket.getData())).trim();
            System.out.println("From client" + clientMessage);
            InetAddress IPaddress = receivepacket.getAddress();
            int port = receivepacket.getPort();
            senddata = ("Received:\"" + clientMessage + "\" from" + IPaddress.getHostName()).getBytes();
            DatagramPacket sendpacket = new DatagramPacket(senddata, senddata.length, IPaddress, port);
            udpserversocket.send(sendpacket);
        }
    }
}
